package services

import (
	"blog-server/app/common/request"
	"blog-server/app/models"
	"blog-server/global"
	"blog-server/utils"
	"errors"
	"gorm.io/gorm"
)

type fontServices struct {
}

var FontService = new(fontServices)

// 获取文章列表
func (fontServices *fontServices) GetArticleList(params request.FrontGetShareList) (list interface{}, err error) {
	switch params.ArtType {
	case 1:
		list, err = GetMusicList(params)
		return
	case 2:
		list, err = GetTechnologyList(params)
		return
	case 3:
		list, err = GetCultureList(params)
		return
	default:
		err = errors.New("未找到文章类型")
		return
	}
}

func GetMusicList(params request.FrontGetShareList) (list []models.MusicShare, err error) {
	err = global.App.DB.Scopes(utils.Paginate(params.PageNum, params.PageSize)).Not("status", 0).Find(&list).Error
	return
}

func GetTechnologyList(params request.FrontGetShareList) (list []models.TechnologyShare, err error) {
	err = global.App.DB.Scopes(utils.Paginate(params.PageNum, params.PageSize)).Not("status", 0).Find(&list).Error
	return
}

func GetCultureList(params request.FrontGetShareList) (list []models.CultureShare, err error) {
	err = global.App.DB.Scopes(utils.Paginate(params.PageNum, params.PageSize)).Not("status", 0).Find(&list).Error
	return
}

// 查看文章详情
func (fontServices *fontServices) GetArticleDetail(params request.FrontGetShareDetail) (detail interface{}, err error) {
	switch params.ArtType {
	case 1:
		detail, err = GetMusicDetail(params)
		return
	case 2:
		detail, err = GetTechnologyDetail(params)
		return
	case 3:
		detail, err = GetCultureDetail(params)
		return
	default:
		err = errors.New("未找到文章类型")
		return
	}
}

func GetMusicDetail(params request.FrontGetShareDetail) (detail models.MusicContent, err error) {
	err = global.App.DB.Preload("MusicShare").First(&detail, params.ContentId).Error
	return
}

func GetTechnologyDetail(params request.FrontGetShareDetail) (detail models.TechnologyContent, err error) {
	err = global.App.DB.Preload("TechnologyShare").First(&detail, params.ContentId).Error
	return
}

func GetCultureDetail(params request.FrontGetShareDetail) (detail models.CultureContent, err error) {
	err = global.App.DB.Preload("CultureShare").First(&detail, params.ContentId).Error
	return
}

// 随机抽取10条数据
func (fontServices *fontServices) GetArticleRandom(params request.FrontGetShareList) (results interface{}, err error) {
	if params.ArtType != 0 {
		err = errors.New("首页请求问题")
		return
	}
	var music []models.MusicShare
	var technology []models.TechnologyShare
	var culture []models.CultureShare
	global.App.DB.Order(gorm.Expr("RAND()")).Limit(10).Find(&music)
	global.App.DB.Order(gorm.Expr("RAND()")).Limit(10).Find(&technology)
	global.App.DB.Order(gorm.Expr("RAND()")).Limit(10).Find(&culture)
	results = []interface{}{music, technology, culture}
	return
}
